package com.xxkk.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.*;

/**
 * @author xxkk
 * @version 1.0.0
 * @title TimeOutTask
 * @description ʕ•ᴥ•ʔ 执行线程会出现超时的任务
 * @create 2024/12/26 22:43
 **/
public class TimeOutTask {
//    private ExecutorService executorService;

//    private TimeOutTask() {
//        this.executorService = new ThreadPoolExecutor(
//                2,
//                Runtime.getRuntime().availableProcessors() * 2,
//                10,
//                TimeUnit.SECONDS,
//                new ArrayBlockingQueue<>(100),
//                new ThreadPoolExecutor.CallerRunsPolicy()
//        );
//    }

    private static String formatTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return sdf.format(new Date(time));
    }

//    public T get(ExecutorService ec, Runnable runnable, int timeOut, TimeUnit timeUnit) {
//        Future<> future = ec.submit(runnable);
//        long startTime = System.currentTimeMillis();
//        System.out.println(timeOut + " 开始时间: " + formatTime(startTime));
////            return future.get(timeOut, timeUnit);
//        return null;
//    }

//    public static void main(String[] args) {
//        ExecutorService executor = Executors.newCachedThreadPool();
//        Callable<Integer> task = () -> {
//            TimeUnit.SECONDS.sleep(2);
//            return 123;
//        };
//
//        Future<Integer> future = executor.submit(task);
//
//        try {
//            System.out.println("Task result: " + future.get()); // 阻塞直到任务完成
//        } catch (InterruptedException | ExecutionException e) {
//            e.printStackTrace();
//        } finally {
//            executor.shutdown();
//        }
//    }


    public static void main(String[] args) {
        BusinessPool.Ins().excute(() -> {
//            try {
//                int x = new ArrayList<Integer>().get(11);
//                System.out.println(x);
//            }catch (Exception e){
//                e.printStackTrace();
//            }
            int x = new ArrayList<Integer>().get(11);
            System.out.println(x);
        });
        for (int i = 10; i > 0; --i) {
            BusinessPool.Ins().excute(() -> {
                System.out.println("1");

            });
        }
        BusinessPool.Ins().getExecutor().shutdown();

    }

    public boolean finalFunTest() {
        try {
            System.out.println("final try");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally exe");
        }
        return false;
    }


}

